﻿using ASoft.Base;
using ASoft.Led;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ASoft.WPF.View
{
    /// <summary>
    /// LedSetting.xaml 的交互逻辑
    /// </summary>
    public partial class LedSetting : Window
    {
        LedService ledService;
        public LedSetting()
        {
            InitializeComponent();
            ledService = new LedService(ConfigInfo.RootPath);
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            ledService.Start(false);
            ledService.LedSendMessage += ledService_LedSendMessage;
            chkLedEnabled.IsChecked = ledService.LedDriveCfg.Enabled;
            txtLedIp.Text = ledService.LedDriveCfg.Ip;
            txtLedPort.Text = ledService.LedDriveCfg.Port.ToString();
            txtLedSwitchSecond.Text = ledService.LedDriveCfg.SwitchSecond.ToString();
            ledCfgDataGrid.ItemsSource = ledService.LedTable.DefaultView;
            txtLedTimeo.Text = ledService.LedDriveCfg.Timeo.ToString();
            txtLedRepeat.Text = ledService.LedDriveCfg.Repeat.ToString();
            LoadPicture();
        }

        void ledService_LedSendMessage(object sender, LedSendMessageArgs e)
        {
            this.Title = "LedSeting:" + e.Message;
        }

        private void Window_Closed_1(object sender, EventArgs e)
        {
            ledService.End();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            ledService.LedDriveCfg.Enabled = chkLedEnabled.IsChecked.Value;
            ledService.LedDriveCfg.Ip = txtLedIp.Text;
            try
            {
                ledService.LedDriveCfg.Port = ushort.Parse(txtLedPort.Text);
                ledService.LedDriveCfg.SwitchSecond = int.Parse(txtLedSwitchSecond.Text);
                ledService.LedDriveCfg.Timeo = uint.Parse(txtLedTimeo.Text);
                ledService.LedDriveCfg.Repeat = uint.Parse(txtLedRepeat.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            ledService.SaveLedCfgFromTable();
            ledService.SaveLedDriveCfg();
            LoadPicture();
        }

        private void LoadPicture()
        {
            var phecs = new List<PhecData>();
            phecs.Add(new PhecData("1#设备", 1, 1));
            phecs.Add(new PhecData("2#设备", 2, 2));
            phecs.Add(new PhecData("3#设备", 3, 3));
            phecs.Add(new PhecData("4#设备", 4, 4));
            phecs.Add(new PhecData("5#设备", 5, 5));
            phecs.Add(new PhecData("6#设备", 6, 6));
            phecs.Add(new PhecData("7#设备", 7, 7));

            var ledPicPhec = ledService.GetLedPic(ELedPic.Phec);
            ledPicPhec.Create(phecs);
            imgPhec.Source = GetBitmapImage(ledPicPhec.SendPicPath);

            //ledService.LedPic.CreatePhec(phecs);

            //ledService.LedPic.CreateRCell(new WeatherData(1, 2, 3, 4, 5),
            //               new RData(11, 12, 13, 14, 15, 16, 17, 18),
            //               new RData(21, 22, 23, 24, 25, 26, 27, 28));
            var ledPicRCell = ledService.GetLedPic(ELedPic.RCell);

            ledPicRCell.Create(new RData(11, 12, 13, 14, 15, 16, 17, 18),
                        new RData(21, 22, 23, 24, 25, 26, 27, 28),
                        new RData(31, 32, 33, 34, 35, 36, 37, 38));
            imgR.Source = GetBitmapImage(ledPicRCell.SendPicPath);

            var weatherData = new WeatherData(1, 2, 3, 4, 5);
            var ledPicWeather = ledService.GetLedPic(ELedPic.Weather);
            ledPicWeather.Create(weatherData);
            imgWeather.Source = GetBitmapImage(ledPicWeather.SendPicPath);

            var ledPicSmile = ledService.GetLedPic(ELedPic.Smile);
            ledPicSmile.Create(null);

        }

        public BitmapImage GetBitmapImage(string path)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.StreamSource = new MemoryStream(File.ReadAllBytes(path));
            bitmap.EndInit();
            bitmap.Freeze();
            return bitmap;
        }

        private void btnSendVSQ_Click(object sender, RoutedEventArgs e)
        {
            ledService.SendVSQ();
        }

        private void btnSendPhec_Click(object sender, RoutedEventArgs e)
        {
            ledService.SendPage2();
        }

        private void btnSendR_Click(object sender, RoutedEventArgs e)
        {
            ledService.SendPage1();
        }
    }
}
